package com.haust.su.ghyy.cmn.controller;

import com.haust.su.ghyy.cmn.service.DictService;
import com.haust.su.ghyy.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.haust.su.ghyy.common.result.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author haustsusu
 * @create 2023-03-05-12:16
 */
@Api(value="数据字典的接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin//此处暂时不使用,现在使用gateway的配置类实现跨域
public class DictController {


    @Autowired
    private DictService dictService;


    //根据数据字典的id查询下级节点
    @ApiOperation(value="查询数据字典内id下的子数据列表(下级节点)")
    @GetMapping("getChildrenDate/{id}")
    public Result getChildrenDate(@PathVariable Long id){
        List<Dict> list = dictService.findChildrenDate(id);
        Result<List<Dict>> ok = Result.ok(list);
        return ok;
    }

    //根据dictcode获取下级节点
    @ApiOperation(value = "根据dictcode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> list=dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }


    //导出数据字典接口操作
    @GetMapping("exportData")
    public void exportData(HttpServletResponse httpServletResponse){
        dictService.exportDictData(httpServletResponse);
    }


    //导入数据字典
    @PostMapping("imporDisttData")
    public Result importDistData(MultipartFile file){
         dictService.importDictData(file);
         return Result.ok();
    }

    /**
     * @Description
     * @author susu haust rg1902
     * @date 2023/4/27 21:17
     * 实现根据dictcode和value查询
     */
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
        String dictName=dictService.getDictName(dictCode,value);
        return dictName;
    }

    //根据value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String dictName=dictService.getDictName("",value);
        return dictName;
    }








}
